
peanut.module.Manager = Ext.extend(Ext.Panel, {
	closable:true,
	mergeUrl:__base+'/admin/module/merge',
	removeUrl:__base+'/admin/module/remove',
	//初始化窗口（用于新增，修改时）,继承后在createWin中调用该方法显示窗口
    initWin:function(width, height, node, status){
		this.win = new Ext.Window({
			node:node,
            title: status,
            width: width,
            height: height,
            modal: true,
            shadow: true,
			iconCls:"addicon",
			//不可以随意改变大小
			resizable:false,
			//是否可以拖动
			//draggable:false,
			defaultType:"textfield",
			labelWidth:100,
			collapsible:true, //允许缩放条
            closeAction: 'hide',
            closable:true,
            plain: true,
			buttonAlign:"center",
			bodyStyle:"padding:10px 0 0 15px",
			listeners:{
			    show:function(w) {
			        //当window show事件发生时清空一下表单
			        this.win.getComponent(0).getForm().setValues(w.node.attributes);
			    },
			    scope:this
			},
            items: [{
            	xtype:'form',
            	plain:true,
				defaultType:"textfield",
				labelWidth:75,
	            baseCls:"x-plain",
				//锚点布局-
				defaults:{anchor:"95%",msgTarget:"side"},
				buttonAlign:"center",
				bodyStyle:"padding:0 0 0 0",
            	items:[{
            		name:'id',
            		xtype:'hidden'
            	},{
            		name:'parentId',
            		xtype:'hidden',
            		value:node.parentNode.id
            	},{
            		readOnly:true,
            		fieldLabel:'上级模块',
            		value:node.parentNode.text
            	},{
            		name:'name',
            		fieldLabel:'名 称'
            	},{
            		name:'type',
            		fieldLabel:'类 型'
            	},{
            		name:'action',
            		fieldLabel:'动 作'
            	},{
            		name:'openView',
            		fieldLabel:'视 图'
            	},{
            		name:'orderNumber',
            		fieldLabel:'顺序'
            	},{
            		xtype:"combo",
            		name:'isDisplay',
            		hiddenName:'isDisplay',
					mode:"local",
					displayField:"show",
					valueField:"value",
					triggerAction:"all",
            		fieldLabel:'是否可见',
            		store:new Ext.data.ArrayStore({
				        fields:["show","value"],
						data:[["显示","0"],["隐藏","1"]]
				    })
            	},{
            		xtype:"combo",
            		name:'enabled',
            		hiddenName:'enabled',
            		fieldLabel:'是否可用',
					mode:"local",
					displayField:"show",
					valueField:"value",
					triggerAction:"all",
            		store:new Ext.data.ArrayStore({
				        fields:["show","value"],
						data:[["可用","0"],["禁用","1"]]
				    })
            	},{
            		xtype:"textarea",
            		height:100,
            		name:'description',
            		fieldLabel:'描 述'
            	},]
            }],
            buttons: [{
                text: "保存",
                handler:function(b,e){
            		this.win.getComponent(0).getForm().submit({
            			waitMsg: '正在保存。。。',
			            url: this.mergeUrl,
			            method: 'POST',
			            success: function(form,action){
			                this.closeWin();
			            },
			            failure:function(form,action) {
			                Ext.Msg.alert("提示!","信息操作失败!");
			            },
			            scope: this
            		});
                },
                scope: this
            }, {
                text: "重置",
                handler:function(){
            		this.win.getComponent(0).getForm().reset();
                },
                scope: this
            }, {
                text: "关闭",
                handler:function(){
            		this.closeWin();
                },
                scope: this
            }]
        });
        return this.win;
    },
    //新增的表单
    addWin:function() {
    	this.win.removeAll();
    	this.win.show();
    },
    //（新增/修改）窗口上的关闭
    closeWin: function() {
        if (this.win) 
            this.win.hide();
        this.win = null;
    },
	initComponent: function(){
  		this.items = [{
			xtype: 'treepanel',
			header:false,
			dataUrl:__base+'/admin/module/all',
			//rootVisible: false,
			root: {
  				id:'0',
				text:'功能模块',
		        nodeType: 'async',
		        expanded:true
		    },
		    expanded:true,
			containerScroll: true,
			contextMenu: new Ext.menu.Menu({
		        items: [{
		        	id:this.id+'add-node',
		        	text:'添加同级模块'
		        },{
		        	id:this.id+'add-child-node',
		        	text:'添加子级模块'
		        },{
		        	id:this.id+'edit-node',
		        	text:'编 辑'
		        },{
		            id: this.id+'delete-node',
		            text: '删 除'
		        }],
		        listeners: {
		            itemclick: function(item) {
                		var n = item.parentMenu.contextNode;
		                switch (item.id) {
		                	case this.id+'add-node':
		                        this.win = this.initWin(300,400,{parentId:n.attributes.parentId,parentNode:n.parentNode},'添加');
		    					this.win.show();
		                        break;
		                    case this.id+'add-child-node':
		                        this.win = this.initWin(300,400,{parentId:n.attributes.id,parentNode:n},'添加子模块');
		    					this.win.show();
		                        break;
		                    case this.id+'edit-node':
		                        this.win = this.initWin(300,400,n,'编辑');
		    					this.win.show();
		                        break;
		                    case this.id+'delete-node':
//		                        Ext.Ajax.request({
//									url: this.removeUrl,
//									method:'POST',
//									headers:{
//		                        		'Content-Type': 'application/json; charset=utf-8'
//									},
//								   	success: function(res, opts){
//										Ext.Msg.alert('揭示','删除成功！');
//								   	},
//								   	failure: function(res, opts){
//								   		Ext.Msg.alert('揭示','删除失败。'+res.responseText);
//								   	},
//								   	jsonData:{id:n.id,parentId:n.parentId,children:{id:n.children.id,parentId:n.children.parentId,action:n.children.action},action:n.action},
//								   	scope:this
//								});
		                    	if (n && n.attributes.id != '0') {
			                        Ext.Ajax.request({
										url: this.removeUrl,
										method:'POST',
									   	success: function(res, opts){
											Ext.Msg.alert('提示','删除成功！');
				                            n.remove();
									   	},
									   	failure: function(res, opts){
									   		Ext.Msg.alert('提示','删除失败。'+res.responseText);
									   	},
									   	params:{id:n.attributes.id},
									   	scope:this
									});
		                        }
		                        break;
		                }
		            },
		            scope:this
		        }
		    }),
			listeners:{
	    		dblclick: function(n,e){
	    		 	this.win = this.initWin(300,400,n,'编辑');
					this.win.show();
		    	},
		    	contextmenu: function(node, e) {
		            node.select();
		            var c = node.getOwnerTree().contextMenu;
	    			c.items.get(this.id+'add-node').setDisabled(node.attributes.id == '0');
	    			c.items.get(this.id+'delete-node').setDisabled(node.attributes.id == '0');
	    			c.items.get(this.id+'edit-node').setDisabled(node.attributes.id == '0');
		            c.contextNode = node;
		            c.showAt(e.getXY());
		        },
		    	scope:this
		    },
		}];
      	peanut.module.Manager.superclass.initComponent.call(this);  
   	}
});
